package ar.com.ford.it.fullEconomics.persistence.dao.query;


import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReportQuery;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese05Paypolvers;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class PrevPaymentPolicyFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        
        Expression exp = null;

        Teese05Paypolvers payPol = (Teese05Paypolvers) parameters[0];
        
        ExpressionBuilder builderReport = new ExpressionBuilder();        
        Expression subQ = null;
        ReportQuery report = new ReportQuery(Teese05Paypolvers.class, builderReport);
        
        report.addMaximum("max-aprovedVersion", builderReport.get("eese05PpyverInDateY"));
        subQ = builderReport.get("teese04Paypolicy").get("eese04PaypolNumberK").equal(payPol.getTeese04Paypolicy().getEese04PaypolNumberK());
        subQ = subQ.and(builder.get("eese05PpyverApprovalUserC").notNull());
        subQ = subQ.and(builder.get("eese05PlantCodeC").equal(payPol.getEese05PlantCodeC()));
        report.setSelectionCriteria(subQ);
        
        exp = builder.get("teese04Paypolicy").get("eese04PaypolNumberK").equal(payPol.getTeese04Paypolicy().getEese04PaypolNumberK());
        exp = exp.and(builder.get("eese05PpyverInDateY").equal(exp.subQuery(report)));
        exp = exp.and(builder.get("eese05PlantCodeC").equal(payPol.getEese05PlantCodeC()));
        
        return exp;
    }
}
